var $$=function(id) {
	return document.getElementById(id);
};
var timer,ok=0,batalie,ceas,f=0,l=0,max1,max2;
function verifica(id){
	valoare=$$(id).value;
	if(valoare!=undefined && valoare.length>4)
	{
		$.ajax({
			url: "check_index.php",
			type: "POST",
			data: "id="+id+"&valoare="+valoare,
			success: function(raspuns){
				if(raspuns=="1") $$(id+"1").src="imagini/ok.png";
				else $$(id+"1").src="imagini/error.png";
			}
		});
	}
}
function verifica_r(element){
	valoare=element.value;
	id=element.id;
	if(valoare!=undefined && (valoare.length>4 || id=="day" || id=="month" || id=="year")){
		$.ajax({
			url: "check_register.php",
			type: "POST",
			data: "id="+id+"&valoare="+valoare,
			success: function(raspuns){
				if(id=="day" || id=="month" || id=="year") id="birday";
				if(raspuns=="1") $$(id+"1").src="imagini/ok.png";
				else $$(id+"1").src="imagini/error.png";
			}
		});
	}
	else $$(id+"1").src="imagini/error.png";
}
function left(id){
	if(id=="f_") 
	{
		val=f;
		m=max1;
	}
	else 
	{
		val=l;
		m=max2;
	}
	if(val>0 && m>=1) 
	{
		$("#"+id+val).hide();
		val--;
		$("#"+id+val).show("fast");
	}
	if(id=="f_") f=val;
	else l=val;
	$$(id).innerHTML=val+1;
}
function right(id){
	if(id=="f_") 
	{
		val=f;
		m=max1;
	}
	else 
	{
		val=l;
		m=max2;
	}
	if(val<m && m>=1) 
	{
		$("#"+id+val).hide();
		val++;
		$("#"+id+val).show("fast");
	}
	if(id=="f_") f=val;
	else l=val;
	$$(id).innerHTML=val+1;
}
function arata(id)
{
	$("#"+id).show("slow");
}
function ascunde(id)
{
	$("#"+id).css({
		"display":"none"
	});
}
function efect_mesaj(id)
{
	$("#"+id).delay(500).fadeIn("slow").delay(5000).fadeOut("slow");
}
function adauga(id)
{
	var valoare=$$(id).value;
	$.ajax({
		url: "post_b.php",
		type: "POST",
		data: "id="+batalie+"&content="+valoare,
		failure: function(){
			alert("Please refresh webpage");
		}
	});
	$$(id).value="";
	$("#"+id).select();
}
function start_battle(battle,time)
{
	batalie=battle;
	$.ajax({
		url: "ceas.php",
		success: function(raspuns){
			ceas=raspuns;
		},
		failure: function(){
			alert("Please refresh webpage");
		}
	});
	timer=setInterval("update_b()",3000);
}
function update_b()
{
	$.ajax({
		url: "refresh_b.php",
		type: "POST",
		data: "id="+batalie+"&ceas="+ceas,
		success: function(raspuns){
			$.ajax({
				url: "ceas.php",
				success: function(raspuns){
					ceas=raspuns;
				},
				failure: function(){
					alert("Please refresh webpage");
				}
			});
			if(raspuns=="-1") //daca s-a terminat batalia
			{
				alert("The battle has ended");
				clearInterval(timer);
				$$("message").readOnly="readonly";
			}
			else if(raspuns=="1") //daca a intrat celalalt jucator in batalie
			{
				location.reload(true);
			}
			else 
			{
				$("#mess").append(raspuns);
				if(raspuns!="") $$("mess").scrollTop=$$("mess").scrollHeight; //daca raspunsul nu este null
			}
		},
		failure: function(){
			alert("Please refresh webpage");
		}
	});
}